/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.workingtimemanager.urb;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import de.workingtimemanager.urb.db.DatabaseHandler;
import de.workingtimemanager.urb.entities.User;
//TODO reenter password field
/**
 *
 * @author CK05102
 */
public class CreateUserActivity extends Activity {

    private DatabaseHandler db;
    private TextView name;
    private TextView password;
    private Button saveButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here
        setContentView(R.layout.createuser);
        db = new DatabaseHandler();
        name = (TextView) findViewById(R.id.createuser_user_text);
        password = (TextView) findViewById(R.id.createuser_password_text);
        saveButton = (Button) findViewById(R.id.createuser_save_button);
        saveButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                createUser(v);
            }
        });
    }

    private void createUser(View v) {
        if (!name.getText().equals(getString(R.string.empty))
                && !password.getText().equals(getString(R.string.empty))) {
            User user = new User(name.getText().toString(), password.getText().toString());

            try{
            db.open(this);
            db.insert(user);
            }
            finally{
            db.close();
            }
            Toast.makeText(this, "User Created", 3000).show();
            this.finish();
        }
    }
}
